import { _decorator, Color, Component, Label, Node, Sprite } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Btn')
export class Btn extends Component {
    /**
     * 道具限定使用次数
     */
    @property({
        displayName:'道具限定使用次数'
    })
    public maxUse: number = 1

    /**
     * 道具可以使用的次数
     */
    @property({
        displayName:'道具可以使用的次数'
    })
    public canUse: number = 0

    private icon: Node = null;
    private dot: Node = null;
    private lable: Label = null;
    //private button: Button = null;
    private sprite: Sprite = null;

    start() {
        //this.button = this.getComponent(Button)
        this.icon = this.node.getChildByName('icon');
        this.sprite = this.icon.getComponent(Sprite)
        this.dot = this.node.getChildByName('dot');
        this.lable = this.dot.getComponentInChildren(Label)
    }

    /**
     * 返回当前按钮可以使用状态
     * @returns 可以使用次数 0|| 可以播放广告获取奖励次数 1 ||按钮禁用2
     */
    public getBtnState() {
        return this.canUse > 0 ? 0 : this.maxUse > 0 ? 1 : 2
    }

    public getCanUse() {
        return this.canUse
    }

    public getMaxUse() {
        return this.maxUse
    }

    public setCanUse(n: number) {
        this.canUse = n
        this.changeData()
    }

    public setMaxUse(n: number) {
        this.maxUse = n
        this.changeData()
    }

    private changeData() {
        this.lable.string = this.canUse > 0 ? this.canUse.toString() : '+'
        this.dot.active = this.maxUse > 0 || this.canUse > 0
        //this.button.interactable = this.dot.active
        this.sprite.color = this.dot.active ? new Color(255, 255, 255) : new Color(150, 150, 150)
    }

    // update(deltaTime: number) {
    //     this.changeData()
    // }
}

